﻿using System;
using System.Collections.Generic;
using System.Windows;
using Yxinmt.Interface;
using Yxinmt.Utility;

namespace Yxinmt.Windows.Meeting
{
    public class Rename : BaseWindow, IWindow
    {
        Entity.FileInfo _File { get; set; }
        public Rename(Window window, Dictionary<string, object> parameters)
        {
            Window = window;
            _Parameters = parameters;
            IsDialogWindow = true;
            _File = parameters.GetValue<Entity.FileInfo>("File");
            InitSet(string.Empty);

            initView();

            this.SetWindow();
        }

        void initView()
        {
            var fileName = Utility.Common.GetProperty(_View, "FileName");
            fileName["Text"] = _File.Name;
        }

        public void CancelDialog(IControl ctl)
        {
            Window.DialogResult = false;
        }

        public string FileName { get; set; }

        public void OkDialog(IControl ctl)
        {
            var name = (string)GetControl("FileName").GetValue();
            if (string.IsNullOrEmpty(name))
            {
                Alert("对不起，文件名不为空");
                return;
            }
            Entity.Response res = null;
            FileName = name + _File.FileType;

            if (FileName == _File.FileName)
            {
                Window.DialogResult = true;
                return;
            }
            if (_File.RecordType == 0)
            {
                //res = RequestService.UpdateFileName(_File.FileId, FileName);
            }
            else
            {
                var record = new Dictionary<string, object>();
                record["FileName"] = FileName;
                record["RecordId"] = _File.FileId;

                res = RequestService.SaveRecord(record, true);
            }
            if (res != null && !res.Result)
            {
                this.Alert(res.Message);
                return;
            }
            Window.DialogResult = true;
        }
    }
}

